package file

import (
	"os"
	"strings"
)

type Path struct {
	list   []string
	length int
	dir    string
	name   string
}

func NewPath(s string) *Path {
	data := &Path{}
	data.list = strings.Split(s, "/")
	data.length = len(data.list)
	data.dir = strings.Join(data.list[:data.length-1], "/") + "/"
	data.name = data.list[data.length-1]

	//if len(strings.TrimSpace(data.name)) == 0 {
	//	data.name = ""
	//}
	return data
}

func (this *Path) DirPath() (string, string) {
	return this.dir, this.name
}

func (this *Path) Dir() string {
	return this.dir
}

func (this *Path) Name() string {
	return this.name
}

func (this *Path) NameSuffix() string {
	n := strings.Index(this.name, ".")
	if n >= 0 && n < len(this.name) {
		return this.name[n:]
	}
	return this.name
}

func (this *Path) NamePrefix() string {
	n := strings.Index(this.name, ".")
	if n >= 0 && n <= len(this.name) {
		return this.name[:n]
	}
	return this.name
}

func (this *Path) IsDir() bool {
	return len(this.Name()) == 0
}

func (this *Path) MkDir() error {
	return os.MkdirAll(this.Dir(), defaultPerm)
}
